<?php
namespace app\admin\model;
use app\index\model\Users;
use app\index\model\Order;
use think\Model;
use think\Db;
use think\Query;
class Win extends Model
{
	public function index(){
		$data = input('post.');
		if($data['ball1']<6){
		    $data['page'] = '跌'; 
            $data['status'] = 0; 
		}else{
		    $data['page'] = '涨'; 
            $data['status'] = 1; 
		}
		$msg = db('result')->where('qishu',$data['qishu'])->find();
		if($msg){
            return ['code' => 0, 'msg' => '该期数已经存在'];
		}else{
		    $data =db('result')->insertGetId($data);
		    if($data){
		    	 //赔率的设置
		        $set = db('set')->field('allodds')->select();
		        $allodds = $set[0]['allodds'];
		        $result = db('result')->field('id,qishu,status')->limit(1)->order('id desc')->select();
		        $id = db('result')->where('id',$data)->find();
		        dump($id);
		        $msg['qishu'] = $id['qishu'];
		        $msg['updown'] = $id['status'];
		        $all = db('order')
		             ->field('oid,uid,SUM(money) as money')
		             ->group('uid')
		             ->where($msg)
		             ->select();
		        foreach ($all as $k => $v) {
		              	 $money = $v['money']*$allodds;
		              	 $wintime = date('Y-m-d H:i:s');
		              	 $update = Db::query("UPDATE `ma_order` SET `status`='1', `winmoney`='$money', `wintime` = '$wintime' WHERE  `oid` = {$v['oid']}");
		              	 $user = db('users')->field('uid,money')->where('uid',$v['uid'])->select();
		              	 foreach ($user as $ks => $vs) {
			                 $money = $vs['money']+$allodds*$v['money'];
			                 $update = Db::query("UPDATE `ma_users` SET `money`='$money' WHERE  `uid` = {$vs['uid']}");
            			  }
		              }      
		    	$content = '添加了开奖期数';
	        	admin_log($content); 
		    	return ['code' => 1, 'msg' => '添加成功'];
		    }else{
		    	return ['code' => 0, 'msg' => '添加失败'];
		    }
		}
	}
}